# -*- coding:utf-8 -*-
'''
@Author: lindes
'''
import json
from django.core.paginator import Paginator
from django.http import JsonResponse
from django.forms.models import model_to_dict
from article.models import Article, PraiseArticle, InformArticle, CollectArticle, CommentArticle
from common.serializers import CommentArticleSerializer, CommentGoodArticleSerializer, CommentPostSerializer
from good_article.models import GoodArticle, PraiseGoodArticle, InformGoodArticle, CollectGoodArticle, \
    CommentGoodArticle
from post.models import PraisePost, Post, InformPost, CollectPost, CommentPost
from users.models import UserProfile
from users.user_utils import check_token


def praise(request, atype, aid):
    code = 0
    msg = 'ok'
    data = 0
    if request.method == 'POST':
        token = request.GET.get('token', None)
        user_id = check_token(token)
        if user_id:
            user = UserProfile.objects.filter(id=user_id).first()
            if user:
                if atype == "article":
                    # 点赞文章
                    article = Article.objects.filter(id=aid).first()
                    if article:
                        pa = PraiseArticle()
                        pa.user = user
                        pa.article = article
                        pa.save()
                        data = 1
                    else:
                        code = 10000
                        msg = "参数值错误"
                elif atype == "good_article":
                    # 点赞识货
                    good_article = GoodArticle.objects.filter(id=aid).first()
                    if good_article:
                        pa = PraiseGoodArticle()
                        pa.user = user
                        pa.article = good_article
                        pa.save()
                        data = 1
                    else:
                        code = 10000
                        msg = "参数值错误"
                elif atype == "post":
                    # 点赞动态
                    post = Post.objects.filter(id=aid).first()
                    if post:
                        pa = PraisePost()
                        pa.user = user
                        pa.article = post
                        pa.save()
                        data = 1
                    else:
                        code = 10000
                        msg = "参数值错误"
                else:
                    code = 10000
                    msg = "参数值错误"
            else:
                code = 20000
                msg = "不存在该用户"
        else:
            code = 10000
            msg = "无效的令牌"
    else:
        code = 40000
        msg = "请求方法错误"
    return JsonResponse({
        'code': code,
        'msg': msg,
        'data': data
    })


def inform(request, atype, aid):
    code = 0
    msg = 'ok'
    data = 0
    if request.method == 'POST':
        token = request.GET.get('token', None)
        user_id = check_token(token)
        if user_id:
            user = UserProfile.objects.filter(id=user_id).first()
            if user:
                if atype == "article":
                    # 举报文章
                    article = Article.objects.filter(id=aid).first()
                    if article:
                        ia = InformArticle()
                        ia.user = user
                        ia.article = article
                        ia.save()
                        data = 1
                    else:
                        code = 10000
                        msg = "参数值错误"
                elif atype == "good_article":
                    # 举报识货
                    good_article = GoodArticle.objects.filter(id=aid).first()
                    if good_article:
                        ia = InformGoodArticle()
                        ia.user = user
                        ia.article = good_article
                        ia.save()
                        data = 1
                    else:
                        code = 10000
                        msg = "参数值错误"
                elif atype == "post":
                    # 举报动态
                    post = Post.objects.filter(id=aid).first()
                    if post:
                        pa = InformPost()
                        pa.user = user
                        pa.article = post
                        pa.save()
                        data = 1
                    else:
                        code = 10000
                        msg = "参数值错误"
                else:
                    code = 10000
                    msg = "参数值错误"
            else:
                code = 20000
                msg = "不存在该用户"
        else:
            code = 10000
            msg = "无效的令牌"
    else:
        code = 40000
        msg = "请求方法错误"
    return JsonResponse({
        'code': code,
        'msg': msg,
        'data': data
    })


def collect(request, atype, aid):
    code = 0
    msg = 'ok'
    data = 0
    if request.method == 'POST':
        token = request.GET.get('token', None)
        user_id = check_token(token)
        if user_id:
            user = UserProfile.objects.filter(id=user_id).first()
            if user:
                if atype == "article":
                    # 收藏文章
                    article = Article.objects.filter(id=aid).first()
                    if article:
                        ca = CollectArticle()
                        ca.user = user
                        ca.article = article
                        ca.save()
                        data = 1
                    else:
                        code = 10000
                        msg = "参数值错误"
                elif atype == "good_article":
                    # 收藏识货
                    good_article = GoodArticle.objects.filter(id=aid).first()
                    if good_article:
                        ca = CollectGoodArticle()
                        ca.user = user
                        ca.article = good_article
                        ca.save()
                        data = 1
                    else:
                        code = 10000
                        msg = "参数值错误"
                elif atype == "post":
                    # 收藏动态
                    post = Post.objects.filter(id=aid).first()
                    if post:
                        ca = CollectPost()
                        ca.user = user
                        ca.article = post
                        ca.save()
                        data = 1
                    else:
                        code = 10000
                        msg = "参数值错误"
                else:
                    code = 10000
                    msg = "参数值错误"
            else:
                code = 20000
                msg = "不存在该用户"
        else:
            code = 10000
            msg = "无效的令牌"
    else:
        code = 40000
        msg = "请求方法错误"
    return JsonResponse({
        'code': code,
        'msg': msg,
        'data': data
    })


def comment(request, atype, aid):
    code = 0
    msg = 'ok'
    data = 0
    if request.method == 'POST':
        token = request.GET.get('token', None)
        user_id = check_token(token)
        if user_id:
            user = UserProfile.objects.filter(id=user_id).first()
            if user:
                comment_id = request.POST.get('comment_id', None)
                content = request.POST.get('content', None)
                if content and len(content) < 250:
                    if atype == "article":
                        # 评论文章
                        article = Article.objects.filter(id=aid).first()
                        if article:
                            ca = CommentArticle()
                            ca.user = user
                            ca.article = article
                            ca.content = content
                            if comment_id != 0:
                                ca.pid = CommentArticle.objects.filter(id=comment_id).first()
                            ca.save()
                            data = 1
                        else:
                            code = 10000
                            msg = "参数值错误"
                    elif atype == "good_article":
                        # 评论识货
                        good_article = GoodArticle.objects.filter(id=aid).first()
                        if good_article:
                            ca = CommentGoodArticle()
                            ca.user = user
                            ca.article = good_article
                            ca.content = content
                            if comment_id != 0:
                                ca.pid = CommentGoodArticle.objects.filter(id=comment_id).first()
                            ca.save()
                            data = 1
                        else:
                            code = 10000
                            msg = "参数值错误"
                    elif atype == "post":
                        # 评论动态
                        post = Post.objects.filter(id=aid).first()
                        if post:
                            ca = CommentPost()
                            ca.user = user
                            ca.article = post
                            ca.content = content
                            if comment_id != 0:
                                ca.pid = CommentGoodArticle.objects.filter(id=comment_id).first()
                            ca.save()
                            data = 1
                        else:
                            code = 10000
                            msg = "参数值错误"
                    else:
                        code = 10000
                        msg = "参数值错误"
                else:
                    code = 10000
                    msg = "参数值错误"
            else:
                code = 20000
                msg = "不存在该用户"
        else:
            code = 10000
            msg = "无效的令牌"
    else:
        code = 40000
        msg = "请求方法错误"
    return JsonResponse({
        'code': code,
        'msg': msg,
        'data': data
    })


def get_comment(request, atype, aid):
    code = 0
    msg = 'ok'
    data = 0
    if request.method == 'GET':
        page = request.GET.get('page', default=1)
        page_size = request.GET.get('page', default=10)
        if atype == "article":
            # 评论文章
            article = Article.objects.filter(id=aid).first()
            if article:
                comment_article = CommentArticle.objects.exclude(is_delete=True).all()
                comment_data = CommentArticleSerializer(comment_article, many=True)
                data = comment_data.data[(page - 1) * page_size: page * page_size]

            else:
                code = 10000
                msg = "参数值错误"
        elif atype == "good_article":
            # 评论识货
            good_article = GoodArticle.objects.filter(id=aid).first()
            if good_article:
                comment_garticle = CommentGoodArticle.objects.exclude(is_delete=True).all()
                comment_data = CommentGoodArticleSerializer(comment_garticle, many=True)
                data = comment_data.data[(page - 1) * page_size: page * page_size]
            else:
                code = 10000
                msg = "参数值错误"
        elif atype == "post":
            # 评论动态
            post = Post.objects.filter(id=aid).first()
            if post:
                comment_post = CommentPost.objects.exclude(is_delete=True).all()
                comment_data = CommentPostSerializer(comment_post, many=True)
                data = comment_data.data[(page - 1) * page_size: page * page_size]
            else:
                code = 10000
                msg = "参数值错误"
        else:
            code = 10000
            msg = "参数值错误"
    else:
        code = 10000
        msg = "参数值错误"
    return JsonResponse({
        'code': code,
        'msg': msg,
        'data': data
    })
